package com.gps.web.dao.sheet;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gps.web.comm.dao.BaseDaoBean;
import com.gps.web.entity.sheets.ClientCorp;

@Repository
public class ClientCorpDaoBean extends BaseDaoBean<ClientCorp, String> implements
ClientCorpDao {
	
	@Override
	@Transactional
	public List<ClientCorp> getEntityByPage(Map<String, Object> conditions) {
		return findByCriteria(getCriteria(conditions));
	}
	
	private Criteria getCriteria(Map<String, Object> conditions) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(ClientCorp.class);

		if(!StringUtils.isBlank((String)conditions.get("corpName"))){
			criteria.add(Restrictions.like("corpName", conditions.get("corpName").toString(), MatchMode.ANYWHERE));
		}
		if(!StringUtils.isBlank((String)conditions.get("corpTaxNum"))){
			criteria.add(Restrictions.eq("corpTaxNum", conditions.get("corpTaxNum").toString()));
		}
		if(!StringUtils.isBlank((String)conditions.get("principal"))){
			criteria.add(Restrictions.like("principal", conditions.get("principal").toString(), MatchMode.ANYWHERE));
		}
		
		return criteria;
	}
}
